import java.util.ArrayList;
import java.util.Random;

import com.sun.jna.Pointer;

///The class implementing gameplay logic.
class AI extends BaseAI
{	
	public String username()
	{
		return "Shell AI";
	}
	public String password()
	{
		return "password";
	}

	//This function is called each time it is your turn
	//Return true to end your turn, return false to ask the server for updated information
	public boolean run()
	{
		// Print out the current board state
		System.out.println("+---+---+---+---+---+---+---+---+");
		for(int rank=8; rank>0; rank--)
		{
			System.out.print("|");
			for(int file=1; file<=8; file++)
			{
				boolean found = false;
				// Loops through all of the pieces
				for(int p=0; !found && p<pieces.length; p++)
				{
					// determines if that piece is at the current rank and file
					if(pieces[p].getRank() == rank && pieces[p].getFile() == file)
					{
						found = true;
						// Checks if the piece is black
						if(pieces[p].getOwner() == Piece.OWNER_BLACK)
						{
							System.out.print("*");
						}
						else
						{
							System.out.print(" ");
						}
						// prints the piece's type
						System.out.print((char)pieces[p].getType()+" ");
					}
				}
				if(!found)
				{
					System.out.print("   ");
				}
				System.out.print("|");
			}
			System.out.println("\n+---+---+---+---+---+---+---+---+");
		}

		// Looks through information about the players
		for(int p=0; p<players.length; p++)
		{
			System.out.print(players[p].getPlayerName());
			// if playerID is 0, you're white, if its 1, you're black
			if(players[p].getId() == playerID())
			{
				System.out.print(" (ME)");
			}
			System.out.println(" time remaining: "+players[p].getTime());
		}

		// if there has been a move, print the most recent move
		if(moves.length > 0)
		{
			System.out.println("Last Move Was:\n"+moves[0]);
		}

		movePiece();

		return true;
	}

	private void movePiece() {
		// select a random piece and move it to a random position on the board.  Attempts to promote to queen if a promotion happens
		Piece piece = pieces[generator.nextInt(pieces.length-1)];
		while(piece.getOwner() != playerID()) piece = pieces[generator.nextInt(pieces.length-1)];

		getValidMoves(piece);

		piece.move(generator.nextInt(7)+1, generator.nextInt(7)+1, (int) 'Q');
	}
	
	private void getValidMoves(Piece piece) {
		Board board = new Board();
		for (int i = 0; i < pieces.length; i++) {
			Piece p = pieces[i];
			board.placePiece(new Cell(p.getFile(), p.getRank()), p);
		}
		
		System.out.println(board.getPieceLocations().toString());
		
		ArrayList<Piece> possiblePieces = new ArrayList<Piece>();

		//if piece is the king
		if(piece.getType() == 'K'){
			for (Piece p : board.getPieceLocations().values()) {
				if(piece != p && testKingAdjacent(piece, p)) possiblePieces.add(p);
			}
			
			System.out.println(possiblePieces.toString());
		}
		//if piece is the queen
		else if(piece.getType() == 'Q'){
			
		}
		//if piece is a bishop
		else if(piece.getType() == 'B'){
			
		}
		//if piece is a knight
		else if(piece.getType() == 'N'){
			
		}
		//if piece is a rook
		else if(piece.getType() == 'R'){
			
		}
		//if piece is a pawn
		else{
			
		}
	}
	
	public boolean testKingAdjacent(Piece king, Piece other){
		boolean adjacentHorizontally = Math.abs(king.distanceBetweenFile(other)) == 1 &&
				king.distanceBetweenRank(other) == 0;
		boolean adjacentVertically = Math.abs(king.distanceBetweenRank(other)) == 1 &&
				king.distanceBetweenFile(other) == 0;
		boolean adjacentDiagonally = Math.abs(king.distanceBetweenFile(other)) == 1 &&
				Math.abs(king.distanceBetweenRank(other)) == 1;
		return adjacentHorizontally || adjacentVertically || adjacentDiagonally;
	}
	
	/*public boolean testPawnMove(Piece pawn, Piece other){
		if (validSetupForMove(board, pawn, emptySquare) &&
				other.inSameFileAs(pawn) &&
				other.distanceBetweenRank(pawn) == forwardDistanceForColor(pawn, 1)) {
			return true;
		}
		else if (pawn.hasMoved() && 
				validSetupForMove(board, currentSquare, emptySquare) &&
				board.clearPathBetween(pawn, other) &&
				other.distanceBetweenRank(pawn) == forwardDistanceForColor(pawn, 2) &&
						other.inSameFileAs(pawn)) {
			return true;
		}
		else {
			return false;
		}
	}*/
	
	private int forwardDistanceForColor(Piece pawn, int forwardDistance) {
		if (pawn.getOwner() == Piece.OWNER_BLACK) {
			return forwardDistance * -1;
		}
		else {
			return forwardDistance;
		}
	}
	
	//This function is called once, before your first turn
	public void init()
	{
		generator = new Random();
	}

	//This function is called once, after your last turn
	public void end() {}


	public AI(Pointer c)
	{
		super(c);
	}

	private Random generator;
}
